package com.johnsoft.pph.gui;

import java.awt.GridBagConstraints;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class GuiSetupSelect extends IGui {
	
	ArrayList<SSFileButton> setupFiles = new ArrayList<SSFileButton>();
	JTextArea errText = new JTextArea();
	SSRetryButton errBut = new SSRetryButton();
	
	public GuiSetupSelect() {
		File f = new File("./");
		String[] files = f.list();
		if(files == null) {
			return;
		}
		for(int i = 0; i < files.length; i++) {
			if(files[i].toLowerCase().endsWith(".pph")) {
				setupFiles.add(new SSFileButton(files[i]));
			}
		}
		errText.setText("The setup coud not find any .phh files!\r\nPlease put the install files in the same folder as this installer!");
	}

	@Override
	JFrame installFrame(JFrame frame) {
		
		GridBagConstraints c = new GridBagConstraints();
		int i;
		for (i = 0; i < setupFiles.size(); i++) {
			c.fill = GridBagConstraints.HORIZONTAL;
			c.gridx = 0;
			c.gridy = i;
			frame.add(setupFiles.get(i), c);
		}
		
		//Error message if they forgot to place the installer files :D
		if(setupFiles.size() == 0) {
			c.fill = GridBagConstraints.HORIZONTAL;
			c.gridx = 0;
			c.gridy = ++i;
			frame.add(errText, c);
			c.gridy = ++i;
			frame.add(errBut, c);
		}
		return frame;
	}

	@Override
	JFrame uninstallFrame(JFrame frame) {
		for(int i = 0; i < setupFiles.size(); i++) {
			frame.remove(setupFiles.get(i));
		}
		frame.remove(errText);
		frame.remove(errBut);
		return frame;
	}

}
